﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace HelloWorld.Startup
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Windows.Controls;

    // HelloWord References
    using HelloWorld.Startup.MVVM;
#if SILVERLIGHT
    using HelloWorld.Silverlight;
#endif

    /// <summary>
    /// Defines the application manager class.
    /// </summary>
    public class ApplicationManager
    {
        //Methods

        /// <summary>
        /// Sets the current view.
        /// </summary>
        /// <typeparam name="T">View's type.</typeparam>
        public static void SetCurrentView<T>()
            where T : View
        {
            ApplicationManager.SetCurrentView<T>(null);
        }

        /// <summary>
        /// Sets the current view for the input <paramref name="contentControl"/>.
        /// </summary>
        /// <typeparam name="T">View's type.</typeparam>
        /// <param name="contentControl">ContentControl where the view'll be hosted.</param>
        public static void SetCurrentView<T>(ContentControl contentControl)
            where T : View
        {
            if (contentControl == null)
            {
#if SILVERLIGHT
                MainPage mainWindow = App.Current.RootVisual as MainPage;
#else
                MainWindow mainWindow = App.Current.MainWindow as MainWindow;
#endif
                if (mainWindow != null)
                {
                    contentControl = mainWindow.ViewContentControl;
                }
            }
            if (contentControl != null)
            {
                View view = Activator.CreateInstance<T>();
                contentControl.Content = ApplicationManager.Compose<View>(view);                
            }
        }
        
        /// <summary>
        /// Composes the input instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static T Compose<T>(T instance) 
            where T : new()
        {
            if (instance != null)
            {
                var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
                var container = new CompositionContainer(catalog);
                container.ComposeParts(instance);

            }
            return instance;
        }
    }
}
